import React, {Component} from 'react';
import './index.less';
import {promptBox, toast, base} from 'nc-lightapp-front';
import ReferSearch from "src/hrpub/common/components/referSearch/org";
// import {Select} from 'tinper-bee';
import handleAllpks from "../../container/functions/handleAllpks";
import NCBackBtn from "src/hrpub/common/components/hr-back/index";

const {NCSelect} = base;
const NCOption = NCSelect.NCOption;

class Header extends Component {
    constructor(props) {
        super(props);
        this.state = {
            printData: {},
            backBtnVisible: false
        };
        this.updateOrgValue = this.updateOrgValue.bind(this);
        this.updateAgreeType = this.updateAgreeType.bind(this);
        this.updateState = this.updateState.bind(this);
        this.onBtnOperation = this.onBtnOperation.bind(this);
        this.deleteAgreementData = this.deleteAgreementData.bind(this);
        this.effectiveAgreementData = this.effectiveAgreementData.bind(this);
        this.saveAgreementData = this.saveAgreementData.bind(this);
        this.goBack = this.goBack.bind(this);

    }

    //按钮点击事件
    onBtnOperation(props, btncode, areacode, opt) {
        this.props.onBtnClickOperation(btncode)
    }

    // 设置返回按钮显示隐藏
    setBackBtnVisible(backBtnVisible) {
        this.setState({
            backBtnVisible
        })
    }

    // 点击返回按钮
    backBtnClick() {
        this.props.setCardListVisible('tableList')
    }

    deleteAgreementData(value) {
        this.props.deleteAgreementData(value)
    }

    effectiveAgreementData(value) {
        this.props.effectiveAgreementData(value)
    }

    saveAgreementData() {
        this.props.saveAgreementData()
    }

    updateOrgValue(value) {
        this.props.updateOrgValue(value);
    }

    updateAgreeType = value => {
        this.props.updateAgreeType(value);
    }

    updateState(value) {
        this.props.updateState(value);
    }

    handleChange = value => {
        this.updateAgreeType(value);
    };

    onBlur = value => {
        console.log(`onBlur`);
    };

    goBack() {
        this.props.updateState({
            btncode: 'back',
            pageFlag: 'list',
            isEdit: false,
            isMain: true
        });
    }

    // 设置卡片分页显示隐藏
    setCardPaginationVisible(paginationShow, pk_agreement) {
        this.setState({
            paginationShow
        }, function () {
            if (pk_agreement) {
                let {cardPagination} = this.props
                cardPagination.setCardPaginationId({id: pk_agreement, status: 1});
            }
        })
    }

    handlePageInfoChange(props, flag) {
        let pks = handleAllpks.get();
        let index = 0
        let pk_agreement = ''
        for (index; index < pks.length; index++) {
            if (pks[index] === flag) {
                pk_agreement = pks[index]
                break;
            }
        }
        this.props && this.props.handleCardPagination(pk_agreement);
    }

    render() {
        const {button, orgVal, dataList, pageFlag, json, isEdit, cardPagination, agreementType} = this.props;
        const {createCardPagination} = cardPagination;
        return (
            <div>
                <div className="header nc-bill-header-area">
                    <div style={{display: pageFlag !== "list" && !isEdit ? true : 'none'}}>
                        <NCBackBtn onClick={this.goBack}/>
                    </div>
                    <div className="header-refer" style={{display: pageFlag === "list" ? true : 'none'}}>
                        {agreementType && <ReferSearch
                            orgVal={orgVal}
                            getOrgData={this.updateOrgValue}
                        />}
                    </div>
                    <div className="nc-select">
                        {json['cm6011-000115'] && dataList.length > 0 ?
                            <NCSelect
                                showSearch
                                style={{width: 150, marginLeft: 6, display: pageFlag === "list" ? true : 'none'}}
                                placeholder={dataList[0].name/*协议类型*/}
                                defaultValue={json['cm6011-000115']/*岗位协议*/}
                                optionFilterProp="children"
                                onSelect={this.onSelect}
                                onChange={this.updateAgreeType}
                                onBlur={this.onBlur}
                            >
                                {
                                    dataList.map(da => <NCOption key={da.code} value={da.code}
                                                                 item={da}>{da.name}</NCOption>)
                                }
                            </NCSelect> : ''
                        }
                    </div>
                    <div className="btn-group">
                        <div className="definition-icons">
                            {button.createButtonApp({
                                area: 'head',
                                buttonLimit: 1,
                                onButtonClick: (props, btncode) => {
                                    this.onBtnOperation(props, btncode, 'head', this);
                                }
                            })}
                            <div className="cardPagination"
                                 style={{display: pageFlag !== "list" && !isEdit ? true : 'none'}}>
                                {
                                    createCardPagination({
                                        handlePageInfoChange: this.handlePageInfoChange.bind(this)
                                    })
                                }
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}

export default Header;
